var RegNumber	= url.templateArgs["RegNumber"];
var RegGroup 	= url.templateArgs["RegGroup"];
var RegTitle	= url.templateArgs["RegTitle"];
var RegNote 	= url.templateArgs["RegNote"];
var Initiator 	= url.templateArgs["Initiator"];
var Mode 		= url.templateArgs["Mode"];
var Documents	= url.templateArgs["Attachments"];

var site = "sakym-personalo-klausimais-registravimas";
var datalistRef = "62449415-9701-4f01-8867-0b548190852c";
var uri = "Sites/" + site + "/dataLists/" + datalistRef;

var referenceType = "node";
var data = new Date();

function returnNode(node){
    var reference = ["workspace", "SpacesStore", node];
    var foundNode = search.findNode(referenceType, reference);
    return foundNode;
}

var personaloPrasymuList = companyhome.childByNamePath(uri); 
var DocArray = Documents.split("|");

var iras = personaloPrasymuList.createNode(null, "rcc:personaloPrasymuList");
iras.properties["rcc:perPrasymRegGroup"] = RegGroup;
iras.properties["rcc:perPrasymRegNumber"] = RegNumber;
iras.properties["rcc:perPrasymRegData"] = data;
iras.properties["rcc:perPrasymRegTitle"] = RegTitle;
iras.properties["rcc:perPrasymRegNote"] = RegNote;
iras.save();
iras.createAssociation(person,"rcc:perPrasymRegistrator");

var iniciator= people.getPerson(Initiator);
if (iniciator != null && iniciator != "" && iniciator != "undefined") {
    iras.createAssociation(iniciator,"rcc:perPrasymIniciator");
}

if (Mode == "multiple") {
	for (var i = 0; i < DocArray.length; i++){
		var reference = ["workspace", "SpacesStore", DocArray[i]];
		var foundNode = search.findNode(referenceType, reference);
		if (foundNode.properties["rcc:dokTip"]  == 100) {
			iras.createAssociation(returnNode(DocArray[i]),"rcc:perPrasymAttachments");
		}
	}
} else {
	for (var i=0;i<DocArray.length;i++){
	   iras.createAssociation(returnNode(DocArray[i]),"rcc:perPrasymAttachments");
	}
}

if (iras.nodeRef != null) {
    status.code=200;
    model.message= "success";
} else {
    status.redirect=true;
    status.code=404;
    model.message= "error";
}